package dev.ece.util.tools;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.StringReader;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;

import org.apache.commons.lang3.StringUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
/**
 * 
 * 说明：常量
 *
 * Constants.java
 *
 * @author DEVILIVED
 * 
 * 版本信息 v1.0
 * 
 * 2013-2-20
 *
 * 修改历史 
 *
 * Copyright 2013, DEVILIVED Inc.
 */
public class Constants {
	
	private static final Logger logger = LogManager.getLogger(Constants.class);

	public static final String getGameDir() {
		String dir = System.getProperty("user.dir");
		return !dir.endsWith(File.separator) ? dir.concat(File.separator) : dir;
	}
	/**
	 * 获得文件路径
	 * @param uri
	 * @return
	 */
	public static String getGameFileDir(String uri) {
		return new File(getGameDir().concat(uri)).getPath();
	}
	
	/**
	 * 获得文件
	 * @param uri
	 * @return
	 */
	public static File getGameFile(String uri) {
		return new File(getGameDir().concat(uri));
	}
	
	/**
	 * 获得文件输入流
	 * @param uri
	 * @return
	 * @throws FileNotFoundException 
	 */
	public static FileInputStream getGameFileInputStream(String uri) throws FileNotFoundException {
		return new FileInputStream(getGameFile(uri));
	}
	
	/**
	 * 读取字符串信息
	 * @param uri
	 * @return
	 * @throws FileNotFoundException
	 */
	public static StringBuffer loadStringSource(String uri) throws FileNotFoundException {
		return loadStringSource(getGameFileInputStream(uri));
	}
	
	/**
	 * 读取字符串信息
	 * @param file
	 * @return
	 * @throws FileNotFoundException
	 */
	public static StringBuffer loadStringSource(File file) throws FileNotFoundException {
		return loadStringSource(new FileInputStream(file));
	}
	
	/**
	 * 读取字符串信息
	 * @param is
	 * @return
	 */
	public static StringBuffer loadStringSource(InputStream is) {
		StringBuffer source = new StringBuffer();
		BufferedReader reader = null;
		reader = new BufferedReader(new InputStreamReader(is));
		String line = null;
		try {
			while ((line = reader.readLine()) != null) {
				source.append(line);
				source.append("\n");
			}
		} catch (IOException e) {
			logger.error("读取出错", e);
		} finally {
			try {
				reader.close();
			} catch (IOException e) {}
		}
		return source;
	}
	
	/**
	 * 替换s中的字符串
	 * @param s
	 * @param functions
	 */
	public static void replace(StringBuffer s, Map<String, String> functions) {
		Iterator<Entry<String, String>> it = functions.entrySet().iterator();
		Entry<String, String> entry = null;
		int start = 0;
		while(it.hasNext()) {
			entry = it.next();
			start = s.indexOf(entry.getKey());
			if(start > -1) {
				s.replace(start, start + entry.getKey().length(), entry.getValue());
			}
		}
	}
	
	public static StringBuilder inline(String content) throws IOException {
		BufferedReader reader = new BufferedReader(new StringReader(content));
		StringBuilder builder = new StringBuilder();
		String line = null;
		while((line = reader.readLine()) != null) {
			if(StringUtils.isNotBlank(line)) {
				builder.append(StringUtils.trim(line)).append(" ");
			}
		}
		return builder;
	}
}
